import {ref} from "vue"
import {ElMessage,ElLoading} from "element-plus";
import type{FormInstance} from "element-plus"
export const useFunc=()=>{
    let formModel = ref ({
        username:"",
        password:"",
        code:"",
        rememberMe:false
    })
    let formRef = ref()
    let code = ref()
    let picCodeRef = ref()
    let globalLoading = ref(false)
    //return {formModel,formRef}

    const handleSubmit = (formRef)=>{
        const loading = ElLoading.service({
            lock:true,
            text:'Loading',
            background:'rgba(0, 0, 0, 0.7)',
        })
        let from = {
            username:formModel.value.username,
            password:formModel.value.password,
        }
        useUser.Login(from).then(res=>{
            globalLoading.value = true;
            setTimeout(()=>{
                loading.close()
                ElMessage({
                    message:'登录成功',
                    type:'success',
                    plain:true,
       
            })
            globalLoading.value = false;
        },2000)
    }).catch(err=>{
        ElMessage({
            message:'登录失败',
            type:'error',
            plain:true,
        })
    })
    }
    const handleReset = (formRef:FormInstance | undefined)=>{
        if(!formRef) return
        formRef.resetFields()
    }
    return{formModel,formRef,code,picCodeRef,globalLoading,handleReset}
}